﻿using IBLL;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class CookBLL : ICookBLL
    {
        public CookBLL(IHeathDAL<CookModel>dal,IHeathDAL<TypeModel>tdal)
        {
            Dal = dal;
            Tdal = tdal;
        }

        public IHeathDAL<CookModel> Dal { get; }
        public IHeathDAL<TypeModel> Tdal { get; }

        public Task<int> AddCook(CookModel cook)
        {
            cook.CreateTime = DateTime.Now;
            return Dal.AddAsync(cook);
        }

        public dynamic CookShow(string? name, int tid)
        {
            var a = Dal.GetAllAsync();
            var b=Tdal.GetAllAsync();
            var sql = from c in a
                      join d in b on c.Tid equals d.Tid
                      select new 
                      {
                          c.Id,
                          c.CookName,
                          c.Img,
                          c.Bq,
                          c.Tid,
                          c.CreateTime,
                          c.Xq,
                          d.TypeName
                      };
            if (!string.IsNullOrEmpty(name)) 
            {
                sql = sql.Where(a => a.CookName.Contains(name));
            }
            if (tid > 0) 
            {
                sql=sql.Where(a=>a.Tid == tid);
            }
            return sql;
        }
    }
}
